﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LawProject.Models;
using Repository.Repositories;
using Repository.Entities;
using Repository.Contracts;
using Core.Interfaces;
using System.IO;

namespace LawProject.Controllers
{
    public class HomeController : Controller
    {
        private IRepository<TBLinhVuc> linhvucRepository;
        private IRepository<TBBanTin> bantinRepository;
        private IRepository<TBBanTinCon> bantinconRepository;
        private IRepository<TBHoTro> hotroRepository;
        private IRepository<TBTinMoi> tinmoiRepository;
        private IRepository<TBTTC> ttcRepository;
        private IRepository<TBUser> userRepository;
        private const int SO_TRANG_HIEN_THI = 1;
        private const int SO_RECORD_OF_TRANG = 2;

        public HomeController()
            : this(IoC.Resolve<IRepository<TBLinhVuc>>(), IoC.Resolve<IRepository<TBBanTin>>(), IoC.Resolve<IRepository<TBBanTinCon>>(), IoC.Resolve<IRepository<TBHoTro>>(), IoC.Resolve<IRepository<TBTinMoi>>(), IoC.Resolve<IRepository<TBTTC>>(), IoC.Resolve<IRepository<TBUser>>())
        {
        }

        public HomeController(IRepository<TBLinhVuc> linhvucRepository, IRepository<TBBanTin> bantinRepository, IRepository<TBBanTinCon> bantinconRepository, IRepository<TBHoTro> hotroRepository, IRepository<TBTinMoi> tinmoiRepository, IRepository<TBTTC> ttcRepository, IRepository<TBUser> userRepository)
        {
            this.linhvucRepository = linhvucRepository;
            this.bantinconRepository = bantinconRepository;
            this.bantinRepository = bantinRepository;
            this.hotroRepository = hotroRepository;
            this.tinmoiRepository = tinmoiRepository;
            this.ttcRepository = ttcRepository;
            this.userRepository = userRepository;
        }
        public ActionResult Index()
        {
            if (Session["lang"]==null)
            {
                Session["lang"] = "en";
            }
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Partial()
        {
            NguyenCustomPage page = new NguyenCustomPage();
            page.TongTrang = 10;
            page.SoTrangHienThi = 5;
            return PartialView(page);
        }

        [HttpGet]
        public ActionResult Partial(int current, int requestId)
        {
            
            NguyenCustomPage page = new NguyenCustomPage();
            page.SoTrangHienThi = SO_TRANG_HIEN_THI;
            page.SoRecord = SO_RECORD_OF_TRANG;
            int tongRecord = int.Parse(ttcRepository.GetOne(a => a.MaTT == "tc").NoiDungEN);
            if (tongRecord % SO_RECORD_OF_TRANG == 0)            
                page.TongTrang = tongRecord / SO_RECORD_OF_TRANG;            
            else            
                page.TongTrang = (tongRecord / SO_RECORD_OF_TRANG) + 1;
            
            int request;
            if (requestId == 0)
                request = 1;
            else if (requestId >= page.TongTrang||requestId==-1)
                request = page.TongTrang;
            else
                request = requestId;
            List<TBTinMoi> listNew = tinmoiRepository.GetSome(a => a.MaTinMoi != null, page.SoRecord, (request - 1) * page.SoRecord).ToList();
            List<ViewBaseTinMoi> listData = new List<ViewBaseTinMoi>();
            foreach (var item in listNew)
            {
                ViewBaseTinMoi tin = new ViewBaseTinMoi { MaTin = item.MaTinMoi.ToString(), Title = item.TieuDeVN };
                tin.Loai = false;
                listData.Add(tin);
            }
            page.XuLyPhanTrang(listData, current, request);
            return PartialView(page);
        }


        public ActionResult Menu()
        {
            
            List<LinhVucClass> resultMenu = new List<LinhVucClass>();
            List<TBLinhVuc> menudata = linhvucRepository.GetAll("STT").ToList();
            if (Session["lang"] == "vn" || Session["lang"] == null)
            {
                foreach (var item in menudata)
                {
                    LinhVucClass lv = new LinhVucClass { MaLinhVuc = item.MaLinhVuc, TenLinhVuc = item.TenLinhVucVN, Loai = item.Loai };
                    resultMenu.Add(lv);
                }
            }
            else
            {
                foreach (var item in menudata)
                {
                    LinhVucClass lv = new LinhVucClass { MaLinhVuc = item.MaLinhVuc, TenLinhVuc = item.TenLinhVucEN, Loai = item.Loai };
                    resultMenu.Add(lv);
                }
            }
            
            return View(resultMenu);
        }

        public ActionResult PhanTrang()
        {
            NguyenCustomPage page = new NguyenCustomPage();
            page.SoTrangHienThi = SO_TRANG_HIEN_THI;
            page.SoRecord = SO_RECORD_OF_TRANG;
            int tongRecord = int.Parse(ttcRepository.GetOne(a => a.MaTT == "tc").NoiDungEN);
            if (tongRecord % SO_RECORD_OF_TRANG == 0)
            {
                page.TongTrang = tongRecord / SO_RECORD_OF_TRANG;
            }
            else
            {
                page.TongTrang = (tongRecord / SO_RECORD_OF_TRANG) + 1;
            }
            List<TBTinMoi> listNew = tinmoiRepository.GetSome(a => a.MaTinMoi != null, page.SoRecord, 0).ToList();
            List<ViewBaseTinMoi> listData = new List<ViewBaseTinMoi>();
            foreach (var item in listNew)
            {
                ViewBaseTinMoi tin = new ViewBaseTinMoi { MaTin = item.MaTinMoi.ToString(), Title = item.TieuDeVN };
                tin.Loai = false;
                listData.Add(tin);
            }
            page.XuLyPhanTrang(listData, 1, 1);
            return View(page);
        }

        public ActionResult ChiTietLinhVuc(string malv)
        {
            var linhvuc = linhvucRepository.GetOne(a => a.MaLinhVuc == malv);
            ViewLinhVucContract result = new ViewLinhVucContract();
            ViewBaseBanTin tinObj = new ViewBaseBanTin();
            if (Session["lang"] == "en")
            {
                result.GioiThieuLV = "Giới thiệu";
                result.TenLinhVuc = linhvuc.TenLinhVucEN;
                var listbantin = bantinRepository.GetSome(a => a.MaLinhVuc == malv).ToList();
                foreach (var item in listbantin)
                {
                    SortBanTin sortbt = new SortBanTin { MaBanTin = item.MaBanTin.ToString(), Title = item.TieuDeEN };
                    if (string.IsNullOrEmpty(sortbt.Title))
                        sortbt.Title = "Loi tieu de";
                    result.ListBanTin.Add(sortbt);
                }
            }
            else
            {
                result.GioiThieuLV = "Giới thiệu EN";
                result.TenLinhVuc = linhvuc.TenLinhVucVN;
                var listbantin = bantinRepository.GetSome(a => a.MaLinhVuc == malv).ToList();
                foreach (var item in listbantin)
                {
                    SortBanTin sortbt = new SortBanTin { MaBanTin = item.MaBanTin.ToString(), Title = item.TieuDeVN };
                    if (string.IsNullOrEmpty(sortbt.Title))
                        sortbt.Title = "Loi tieu de";
                    result.ListBanTin.Add(sortbt);
                }
            }
            Session["Control"] = "Home";
            Session["Action"] = "ChiTietLinhVuc";
            Session["Name"] = "malv";
            Session["Value"] = malv;
            return View(result);
        }

        public ActionResult ChiTietBanTin(string mabt)
        {
            ViewBanTinContract result = new ViewBanTinContract();            
            var bantin = bantinRepository.GetOne(a => a.MaBanTin == new Guid(mabt));
            var linhvuc = linhvucRepository.GetOne(a => a.MaLinhVuc == bantin.MaLinhVuc);
            var listbantin = bantinRepository.GetSome(a => a.MaLinhVuc == bantin.MaLinhVuc).ToList();
            var listbantincon = bantinconRepository.GetSome(a => a.MaBanTin == bantin.MaBanTin).ToList();
            if (Session["lang"] == "en")
            {
                result.TenLinhVuc = linhvuc.TenLinhVucEN;
                ViewBaseBanTin tin = new ViewBaseBanTin();
                tin.MaBanTin = bantin.MaBanTin.ToString();
                tin.MaThuMucCha = bantin.MaLinhVuc;
                tin.NgayDT = bantin.NgayDT.HasValue ? bantin.NgayDT.ToString() : "Unknow";
                tin.NguoiDT = bantin.NguoiDT;
                tin.TieuDe = bantin.TieuDeEN;
                tin.TomTat = bantin.TomTatEN;
                tin.NoiDung = bantin.NoiDungEN;
                result.BanTin = tin;
                foreach (var item in listbantin)
                {
                    SortBanTin sortbt = new SortBanTin { MaBanTin = item.MaBanTin.ToString(), Title = item.TieuDeEN };
                    if (string.IsNullOrEmpty(sortbt.Title))
                        sortbt.Title = "Loi tieu de";
                    result.ListBanTinLQ.Add(sortbt);
                }

                foreach (var item in listbantincon)
                {
                    SortBanTinCon sortbt = new SortBanTinCon { MaBanTinCon = item.MaBTC.ToString(), Title = item.TieuDeEN };
                    if (string.IsNullOrEmpty(sortbt.Title))
                        sortbt.Title = "Loi tieu de";
                    result.ListBanTinCon.Add(sortbt);
                }
            }
            else
            {
                result.TenLinhVuc = linhvuc.TenLinhVucVN;
                ViewBaseBanTin tin = new ViewBaseBanTin();
                tin.MaBanTin = bantin.MaBanTin.ToString();
                tin.MaThuMucCha = bantin.MaLinhVuc;
                tin.NgayDT = bantin.NgayDT.HasValue ? bantin.NgayDT.ToString() : "Không xác định";
                tin.NguoiDT = bantin.NguoiDT;
                tin.TieuDe = bantin.TieuDeVN;
                tin.TomTat = bantin.TomTatVN;
                tin.NoiDung = bantin.NoiDungVN;
                result.BanTin = tin;
                foreach (var item in listbantin)
                {
                    SortBanTin sortbt = new SortBanTin { MaBanTin = item.MaBanTin.ToString(), Title = item.TieuDeVN };
                    if (string.IsNullOrEmpty(sortbt.Title))
                        sortbt.Title = "Loi tieu de";
                    result.ListBanTinLQ.Add(sortbt);
                }

                foreach (var item in listbantincon)
                {
                    SortBanTinCon sortbt = new SortBanTinCon { MaBanTinCon = item.MaBTC.ToString(), Title = item.TieuDeVN };
                    if (string.IsNullOrEmpty(sortbt.Title))
                        sortbt.Title = "Loi tieu de";
                    result.ListBanTinCon.Add(sortbt);
                }
            }
            return View(result);
        }

        public ActionResult ChiTietBanTinCon(string mabtc)
        {
            ViewBanTinContract result = new ViewBanTinContract();
            var bantin = bantinconRepository.GetOne(a => a.MaBTC == new Guid(mabtc));
            var btcha = bantinRepository.GetOne(a => a.MaBanTin == bantin.MaBanTin);
            var linhvuc = linhvucRepository.GetOne(a=>a.MaLinhVuc == btcha.MaLinhVuc);
            var listbantin = bantinRepository.GetSome(a=>a.MaLinhVuc == linhvuc.MaLinhVuc).ToList();
            var listbantincon = bantinconRepository.GetSome(a => a.MaBanTin == bantin.MaBanTin);
            
            if(Session["lang"]=="en")
            {
                ViewBaseBanTin tin = new ViewBaseBanTin();
                tin.MaBanTin = bantin.MaBTC.ToString();
                tin.MaThuMucCha = bantin.MaBanTin.ToString();
                tin.NgayDT = bantin.NgayDT.HasValue ? bantin.NgayDT.ToString() : "Unknow";
                tin.NguoiDT = bantin.NguoiDT;
                tin.TieuDe = bantin.TieuDeEN;
                tin.TomTat = bantin.TomTatEN;
                tin.NoiDung = bantin.NoiDungEN;
                result.BanTin = tin;
                result.TenLinhVuc = linhvuc.TenLinhVucEN;
                foreach (var item in listbantin)
                {
                    SortBanTin sortbt = new SortBanTin { MaBanTin = item.MaBanTin.ToString(), Title = item.TieuDeEN };
                    result.ListBanTinLQ.Add(sortbt);
                }

                foreach (var item in listbantincon)
                {
                    SortBanTinCon sortbt = new SortBanTinCon { MaBanTinCon = item.MaBTC.ToString(), Title = item.TieuDeEN };
                    result.ListBanTinCon.Add(sortbt);
                }
            }
            else
            {
                ViewBaseBanTin tin = new ViewBaseBanTin();
                tin.MaBanTin = bantin.MaBTC.ToString();
                tin.MaThuMucCha = bantin.MaBanTin.ToString();
                tin.NgayDT = bantin.NgayDT.HasValue ? bantin.NgayDT.ToString() : "Không biết";
                tin.NguoiDT = bantin.NguoiDT;
                tin.TieuDe = bantin.TieuDeVN;
                tin.TomTat = bantin.TomTatVN;
                tin.NoiDung = bantin.NoiDungVN;
                result.BanTin = tin;
                result.TenLinhVuc = linhvuc.TenLinhVucVN;
                foreach (var item in listbantin)
                {
                    SortBanTin sortbt = new SortBanTin { MaBanTin = item.MaBanTin.ToString(), Title = item.TieuDeVN };
                    result.ListBanTinLQ.Add(sortbt);
                }

                foreach (var item in listbantincon)
                {
                    SortBanTinCon sortbt = new SortBanTinCon { MaBanTinCon = item.MaBTC.ToString(), Title = item.TieuDeVN };
                    result.ListBanTinCon.Add(sortbt);
                }
            }
            return View(result);
        }

        public ActionResult ThongTinChung(string matt)
        {
            TBTTC ttc = ttcRepository.GetOne(a => a.MaTT == matt);
            List<TBTTC> tttttt = ttcRepository.GetAll().ToList();
            int u = tttttt.Count;
            ViewBaseTTC result = new ViewBaseTTC();
            result.MaTT = ttc.MaTT;
            if (Session["lang"] == "en")
            {
                result.NoiDung = ttc.NoiDung;
            }
            else
            {
                result.NoiDung = ttc.NoiDungEN;
            }
            return View(result);
        }

        public ActionResult TraoDoi()
        {
            TBTTC ttc = ttcRepository.GetOne(a => a.MaTT == "TRAODOI");
            var listMenu = linhvucRepository.GetSome(a => a.Loai == 3).ToList();
            ViewBaseTTC result = new ViewBaseTTC();
            result.MaTT = ttc.MaTT;
            return View();
        }

        public ActionResult ChangeLangVN()
        {
            if (Session["lang"]==null)
            {
                Session["lang"] = "vn";
            }
            else
            {
                if (Session["lang"].ToString() == "en")
                    Session["lang"] = "vn";
            }
            return RedirectToAction("Index");
        }

        public ActionResult ChangeLangEN()
        {
            if (Session["lang"] == null)
            {
                Session["lang"] = "vn";
            }
            else
            {
                if (Session["lang"].ToString() == "vn")
                    Session["lang"] = "en";
            }
            return RedirectToAction("Index");
        }

        
    }
}
